package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.dto.MenuComponent;
import com.fms.dto.MenuDto;
import com.fms.entity.Menu;
import com.fms.entity.MenuCheck;
import com.fms.entity.SysUser;
import com.fms.mapper.MenuCheckMapper;
import com.fms.mapper.MenuMapper;
import com.fms.mapper.SysUserMapper;
import com.fms.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    private final MenuMapper menuMapper;

    private final MenuCheckMapper menuCheckMapper;

    private final SysUserMapper sysUserMapper;

    @Override
    //获取所有菜单列表
    public List<Menu> getMenus() {
        LambdaQueryWrapper<Menu> query = new LambdaQueryWrapper<>();
        query.ne(Menu::getStatus,0);
        return menuMapper.selectList(query);
    }

    @Override
    //获取部门菜单内容
    public List<MenuDto> getMenuOption(String username) {
        //根据姓名获取部门id
        LambdaQueryWrapper<SysUser> sysQuery = new LambdaQueryWrapper<>();
        sysQuery.eq(SysUser::getUsername, username);
        Long id = sysUserMapper.selectOne(sysQuery).getDeptId();
        //获取菜单对应权限
        LambdaQueryWrapper<MenuCheck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MenuCheck::getDeptId, id);
        List<MenuCheck> menuChecks = menuCheckMapper.selectList(queryWrapper);

        LambdaQueryWrapper<Menu> query = new LambdaQueryWrapper<>();
        List<Menu> menus = menuMapper.selectList(query);
        //组装前端菜单数据
        List<MenuDto> result = new ArrayList<>();
        for (Menu menu : menus) {
            //一级菜单
            if(menu.getStatus() == 0){
                MenuDto dto = new MenuDto();
                dto.setLabel(menu.getMenuName());
                List<MenuComponent> list = new ArrayList<>();
                //二级菜单
                for (MenuCheck check : menuChecks) {
                    for (Menu menu1 : menus){
                        if (check.getMenuId().equals(menu1.getId()) && menu1.getStatus().equals(menu.getId())){
                            List<MenuComponent> list2 = new ArrayList<>();
                            MenuComponent component = new MenuComponent();
                            //三级菜单
                            for (Menu menu2 : menus){
                                for (MenuCheck checks : menuChecks) {
                                    if (checks.getMenuId().equals(menu2.getId()) && menu2.getStatus().equals(menu1.getId())) {
                                        MenuComponent component2 = new MenuComponent();
                                        component2.setLabel(menu2.getMenuName());
                                        component2.setIcon(menu2.getIcon());
                                        component2.setTo(menu2.getToPath());
                                        list2.add(component2);
                                    }
                                }
                            }
                            if (list2.size()==0){
                                component.setLabel(menu1.getMenuName());
                                component.setIcon(menu1.getIcon());
                                component.setTo(menu1.getToPath());
                            }else {
                                component.setLabel(menu1.getMenuName());
                                component.setIcon(menu1.getIcon());
                                component.setItems(list2);
                            }
                            list.add(component);
                        }
                    }
                }
                dto.setItems(list);
                result.add(dto);
            }
        }
        return result;
    }
}
